//
//  dispatch_main.c
//  iChatStockRelayServer
//
//  Created by pohanlee on 2014/5/15.
//  Copyright (c) 2014年 iChat. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/errno.h>
#include <unistd.h>
#include <arpa/inet.h>

#define SERV_PORT       5566
#define MAXNAME         1024
#define MAX_LENGTH      1024

extern int errno;

static pthread_t dispatch_thread;

void* dispatch_main(void)
{
    int socket_fd;      /* file description into transport */
    int recfd;          /* file descriptor to accept        */
    int length;     /* length of address structure      */
    int nbytes;     /* the number of read **/
    char recvBuf[MAX_LENGTH], sendBUf[MAX_LENGTH];
    struct sockaddr_in server_addr; /* address of this service */
    struct sockaddr_in client_addr; /* address of client    */
    /*
     *      Get a socket into TCP/IP
     */
    if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) <0) {
        perror ("socket failed");
        exit(1);
    }
    /*
     *    Set up our address
     */
    bzero ((char *)&server_addr, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(SERV_PORT);
    
    
    /*
     *     Bind to the address to which the service will be offered
     */
    if (bind(socket_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) <0) {
        perror ("bind failed");
        exit(1);
    }
    
    
    /*
     * Set up the socket for listening, with a queue length of 5
     */
    if (listen(socket_fd, 100) <0) {
        perror ("listen failed");
        exit(1);
    }
    /*
     * Loop continuously, waiting for connection requests
     * and performing the service
     */
    length = sizeof(client_addr);
    printf("Server is ready to receive !!\n");
    printf("Can strike Cntrl-c to stop Server >>\n");
    while (1) {
        if ((recfd = accept(socket_fd,
                            (struct sockaddr*)&client_addr, (socklen_t*)&length)) <0) {
            perror ("could not accept call");
            exit(1);
        }
        
        
        if ((nbytes = read(recfd, recvBuf, BUFSIZ)) < 0) {
            perror("read of data error nbytes !");
            exit (1);
        }
        
        printf("Create socket #%d form %s : %d\n", recfd,
               inet_ntoa(client_addr.sin_addr), htons(client_addr.sin_port));
        printf("%s\n", recvBuf);
        
        /* return to client */
        sprintf(sendBUf, "200 OK\n\n");
        if (write(recfd, &sendBUf, nbytes) == -1) {
            perror ("write to client error");
            exit(1);
        }
        close(recfd);
        printf("Can Strike Crtl-c to stop Server >>\n");
    }

}



int main(int argc, char* argv[])
{
    int ret;
    
    ret = pthread_create(&dispatch_thread, NULL, (void*(*)(void*)) dispatch_main, NULL);
    if (ret < 0) {
        perror("pthread_create:");
        exit(1);
    }
    
    pthread_join(dispatch_thread, NULL);
    
    return 0;
    
}

